package com.frontend.screens;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.frontend.*;
import com.users.User;

/**
 * This screen allows a Nurse, Doctor, or Admin to create a new Patient, and allows
 * Admins to create any type of new user
 * 
 * @author Elizabeth
 * @author Taylor
 *
 */
public class CreateUserScreen extends Screen {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9065571645658567076L;
	private JLabel message, nameLabel, usernameLabel, newPasswordLabel,
			confirmNewPasswordLabel, user_typeLabel;
	private JPanel mainPanel, buttonsPanel;
	private JPanel b1, b2, b5, b6;
	private JComboBox typeList;
	private JTextField usernameField, nameField;
	private JPasswordField newPassword, confirmNewPassword;
	private JButton create, back;
	private boolean block;
	private javax.swing.Timer timer;
	private int cycles;

	/**
	 * Creates the screen to create a new user
	 * 
	 * @param window
	 */
	public CreateUserScreen(DoctorsOfficeClient window) {
		super(window);

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		timer = new javax.swing.Timer(50, new AnimationListener());
		LoginListener ll = new LoginListener();

		b1 = new JPanel();
		b1.setOpaque(false);

		message = new JLabel("Create New User");
		message.setAlignmentX(CENTER_ALIGNMENT);

		b2 = new JPanel();
		b2.setOpaque(false);

		nameLabel = new JLabel("Name:  ");
		nameLabel.setHorizontalAlignment(SwingConstants.TRAILING);
		nameField = new JTextField();
		nameField.addActionListener(ll);

		usernameLabel = new JLabel("Username:  ");
		usernameLabel.setHorizontalAlignment(SwingConstants.TRAILING);
		usernameField = new JTextField();
		usernameField.addActionListener(ll);

		newPasswordLabel = new JLabel("New Password:  ");
		newPasswordLabel.setHorizontalAlignment(SwingConstants.TRAILING);
		newPassword = new JPasswordField();
		newPassword.addActionListener(ll);

		confirmNewPasswordLabel = new JLabel("Confirm New Password:  ");
		confirmNewPasswordLabel.setHorizontalAlignment(SwingConstants.TRAILING);
		confirmNewPassword = new JPasswordField();
		confirmNewPassword.addActionListener(ll);

		user_typeLabel = new JLabel("User Type:  ");
		user_typeLabel.setHorizontalAlignment(SwingConstants.TRAILING);
		if (window.getCurrentUser().getUser_type() == User.ADMIN)
			typeList = new JComboBox(new String[] { "Admin", "Doctor", "Nurse",
					"Patient" });
		else
			typeList = new JComboBox(new String[] { "Patient" });
		typeList.setOpaque(false);

		mainPanel = new JPanel();
		mainPanel.setOpaque(false);
		mainPanel.setLayout(new GridLayout(9, 2));
		mainPanel.add(nameLabel);
		mainPanel.add(nameField);
		mainPanel.add(Box.createGlue());
		mainPanel.add(Box.createGlue());
		mainPanel.add(usernameLabel);
		mainPanel.add(usernameField);
		mainPanel.add(Box.createGlue());
		mainPanel.add(Box.createGlue());
		mainPanel.add(newPasswordLabel);
		mainPanel.add(newPassword);
		mainPanel.add(Box.createGlue());
		mainPanel.add(Box.createGlue());
		mainPanel.add(confirmNewPasswordLabel);
		mainPanel.add(confirmNewPassword);
		mainPanel.add(Box.createGlue());
		mainPanel.add(Box.createGlue());
		mainPanel.add(user_typeLabel);
		mainPanel.add(typeList);

		b5 = new JPanel();
		b5.setOpaque(false);

		buttonsPanel = new JPanel();
		buttonsPanel.setOpaque(false);
		create = new JButton("Create User");
		create.setOpaque(false);
		create.setAlignmentX(Component.CENTER_ALIGNMENT);
		create.addActionListener(ll);
		back = new JButton("Go Back");
		back.setOpaque(false);
		back.setAlignmentX(Component.CENTER_ALIGNMENT);
		back.addActionListener(ll);

		buttonsPanel.add(create);
		buttonsPanel.add(back);

		b6 = new JPanel();
		b6.setOpaque(false);

		add(b1);
		add(message);
		add(b2);
		// add(currentPasswordPanel);
		// add(b3);
		// add(newPasswordPanel);
		// add(b4);
		// add(confirmNewPasswordPanel);
		add(mainPanel);
		add(b5);
		add(buttonsPanel);
		// add(b6);
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (getHeight() != tempheight || getWidth() != tempwidth) {
			message.setFont(new Font("Arial", Font.BOLD, getHeight() / 15));

			nameLabel.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			nameField.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			nameField.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			usernameLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			usernameField.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			usernameField.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));

			newPasswordLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			newPassword.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			newPassword
					.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));

			confirmNewPasswordLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			confirmNewPassword.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			confirmNewPassword.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));

			user_typeLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			typeList.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			typeList.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));

			mainPanel.setMaximumSize(new Dimension(getWidth() * 9 / 10,
					getHeight()));

			create.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 15));
			create.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			back.setMaximumSize(new Dimension(getWidth() / 5, getHeight() / 15));
			back.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));

			tempheight = getHeight();
			tempwidth = getWidth();
		}
	}

	private class LoginListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(back)) {
				window.changeScreen(new NavigationScreen(window));
			} else {
				NetworkInterfacer net = window.getNet();
				String name = nameField.getText();
				String username = usernameField.getText();
				String newPwd = new String(newPassword.getPassword());
				String confNewPwd = new String(confirmNewPassword.getPassword());
				int user_type = 0;
				if (typeList.getSelectedItem().equals("Doctor"))
					user_type = User.DOCTOR;
				else if (typeList.getSelectedItem().equals("Nurse"))
					user_type = User.NURSE;
				else if (typeList.getSelectedItem().equals("Patient"))
					user_type = User.PATIENT;
				block = false;

				if (username.equals("") || newPwd.equals("")
						|| confNewPwd.equals("")) {
					block = true;
					showError("Fields cannot be blank");
				}
				if (!block) {
					if (!newPwd.equals(confNewPwd)) {
						block = true;
						showError("Passwords do not Match.");
					}
					if (!block) {
						if (name.length() > 40) {
							block = true;
							showError("Name too long (Max 40)");
						} else if (username.length() > 20) {
							block = true;
							showError("Username too long (Max 20)");
						} else if (newPwd.length() > 50) {
							block = true;
							showError("Password too long (Max 50)");
						}
					}
					if (!block) {
						try {
							if (net.addUser(name, username, newPwd, user_type)) {
								window.changeScreen(new NavigationScreen(window));
							} else {
								block = true;
								showError("Username Already In Use");
							}
						} catch (ConnectionException e1) {
							System.out.println(e1.getMessage());
						}
					}
				}
			}
		}
	}
	
	private void showError(String line){
		timer.stop();
		block = true;
		cycles = 0;
		message.setText(line);
		message.setForeground(Color.red);
		timer.start();
	}

	private class AnimationListener implements ActionListener {
		private int alphaVal;
		private boolean fadeout, fadein;

		@Override
		public void actionPerformed(ActionEvent e) {
			if (cycles == 0){
				alphaVal = 255;
				fadeout = true;
				fadein = false;
			}
			cycles++;

			if (cycles > 20) {
				if (fadeout) {
					alphaVal = alphaVal - 10;
					if (alphaVal < 0)
						alphaVal = 0;
					message.setForeground(new Color(255, 0, 0, alphaVal));
					if (alphaVal == 0) {
						fadeout = false;
						fadein = true;
						message.setText("Welcome to the Doctor's Office!");
					}
				}
				if (fadein) {
					alphaVal += 10;
					if (alphaVal > 255)
						alphaVal = 255;
					message.setForeground(new Color(0, 0, 0, alphaVal));
					if (alphaVal == 255) {
						fadein = false;
						timer.stop();
					}
				}
			}
		}

	}

}
